package com.nartosoft.tubuloader;

import java.io.File;

import net.jimmc.jshortcut.JShellLink;

/**
 * Creates and destroys PicUploader links in the current user's SendTo directory. 
 */
public class LinkManager {	
//	private final static String RELATIVE_JAR_PATH = "\\picuploader.jar"; // read from 'installer.config' file?
	//private final static String RELATIVE_ICON_PATH = "\\res\\icon.ico";  // read from 'installer.config' file? should be user defineable
	
	private final static String EXECUTION_PATH = System.getProperty("user.dir");
//	private final static String JAR_PATH = EXECUTION_PATH + RELATIVE_JAR_PATH;
	private final static String SENDTO_PATH = System.getProperty("user.home") + "\\SendTo";
	//private final static String ICON_PATH = EXECUTION_PATH + RELATIVE_ICON_PATH; should be userdefinable, too
	
	private LinkManager() {}
	
	/**
	 * Creates a new link file in the current user's SendTo folder. The link has
	 * the PicUploader icon and points to PicUploader.jar.<br>
	 * Assumes that the .jar and .ico files exist at RELATIVE_JAR_PATH and 
	 * RELATIVE_ICON_PATH relative to the current execution directory.
	 * @param linkName the name of the link file
	 * @param arguments the arguments to PicUploader.jar for this link
	 */	
	public static void createSendToLink(String linkName, String arguments, String iconPath) {
		final JShellLink link = new JShellLink(SENDTO_PATH, linkName);
		link.setPath("javaw.exe");
		link.setWorkingDirectory(EXECUTION_PATH);
		link.setArguments(arguments);
		link.setIconLocation(iconPath);
		link.save();
	}
	
	/**
	 * Deletes a link file in the current user's SendTo directory.
	 * @param linkName the name of the link (without .lnk file extension)
	 */
	public static void deleteSendToLink(String linkName) {
		final File linkFile = new File(SENDTO_PATH + "\\" + linkName + ".lnk");
		if (!linkFile.exists()) {
			return;
		}
		final boolean success = linkFile.delete();
		if (!success || linkFile.exists()) {
			linkFile.deleteOnExit();
		}
	}
}
